#!/bin/bash

cd /tmp
if [ ! -f php-pkg.tar.gz ]; then
  echo "请输入您要安装的PHP版本号(例如 7.0.6):"
  read phpVersion

  echo "开始下载安装包"
  wget -c "http://cn2.php.net/distributions/php-$phpVersion.tar.gz" -O php-pkg.tar.gz
  if [ $? -ne 0 ]; then
    echo "PHP安装文件下载失败，请检查下载地址是否有效！"
    echo "http://cn2.php.net/distributions/php-$phpVersion.tar.gz"
    rm -f /tmp/php-pkg.tar.gz
    exit
  fi
fi

echo "正在解压文件...."
sleep 2
rm -fr /tmp/php-pkg
mkdir /tmp/php-pkg && tar zxvf php-pkg.tar.gz -C php-pkg --strip-components 1
cd ./php-pkg

echo "开始安装PHP"
sleep 2
./configure --prefix=/usr/local/php --with-iconv --with-zlib --enable-xml --disable-rpath --enable-maintainer-zts --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-mysql --with-mysqli --with-pdo-mysql --enable-ftp --with-jpeg-dir --with-freetype-dir --with-png-dir --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-opcache=no 


make clean
make -j 2
make install

ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/

#config
cp ./php.ini-production /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
useradd www

cd /usr/local/php/lib
sed '/; extension_dir = "ext"/a\extension_dir="/usr/local/php/lib/php/extensions/no-debug-zts-20121212"' php.ini > php.ini.tmp
cat php.ini.tmp > php.ini
rm -f *.tmp

echo "恭喜，PHP已安装成功！"



